package xin.lhuo.we.server.controller.user;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import xin.lhuo.we.server.entity.User;
import xin.lhuo.we.server.enums.ResultEnum;
import xin.lhuo.we.server.service.IDocumentService;
import xin.lhuo.we.server.utils.ResultVoUtil;
import xin.lhuo.we.server.vo.ResultVo;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

import static xin.lhuo.we.server.constant.HttpConstant.SESSION_USER;

/**
 * @author Lhuo
 * @date 2019/5/25 16:22
 */
@RestController
@Slf4j
public class DocumentController {
    private final IDocumentService iDocumentServer;

    public DocumentController(IDocumentService iDocumentServer) {
        this.iDocumentServer = iDocumentServer;
    }

    /**
     * 创建文档目录
     *
     * @param name
     * @param request
     * @return
     */
    @PostMapping(value = "/folder")
    public ResultVo createFolder(@RequestParam String name, HttpServletRequest request) {
        if (StringUtils.isEmpty(name)) {
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "文件名不能为空");
        }
        iDocumentServer.createFolder(getUser(request).getId(), name);
        return ResultVoUtil.success();
    }

    /**
     * 获取文档目录列表
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/folder")
    public ResultVo getFolder(HttpServletRequest request) {
        return ResultVoUtil.success(iDocumentServer.getFolder(getUser(request).getId()));
    }

    /**
     * 文档目录重命名
     *
     * @param id
     * @param name
     * @param request
     * @return
     */
    @PutMapping(value = "/folder/rename")
    public ResultVo renameFolder(@RequestParam Integer id, @RequestParam String name, HttpServletRequest request) {
        if (id == null) {
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "id不能为空");
        }
        if (StringUtils.isEmpty(name)) {
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "文件名不能为空");
        }
        iDocumentServer.renameFolder(id, name, getUser(request).getId());
        return ResultVoUtil.success();
    }

    /**
     * 删除文档目录
     *
     * @param id
     * @param request
     * @return
     */
    @DeleteMapping(value = "/folder")
    public ResultVo deleteFolder(@RequestParam Integer id, HttpServletRequest request) {
        if (id == null) {
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "id不能为空");
        }
        iDocumentServer.deleteFolder(id, getUser(request).getId());
        return ResultVoUtil.success();
    }

    /**
     * 获取七牛上传token
     *
     * @return
     */
    @GetMapping(value = "/document/update_token")
    public ResultVo getQiNiuUpdateToken() {
        String token = iDocumentServer.getUpdateToken();
        HashMap<String, String> result = new HashMap<>(1);
        result.put("token", token);
        return ResultVoUtil.success(result);
    }

    @PostMapping("/document/callback")
    public HashMap uploadCallback(HttpServletRequest request, HttpServletResponse response) {
        HashMap<String, String> resultMap = new HashMap<>(1);
        try {
            String authorization = request.getHeader("Authorization");
            ServletInputStream inputStream = request.getInputStream();
            iDocumentServer.saveDocument(authorization, inputStream);
            resultMap.put("res", "success");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(500);
            resultMap.put("res", "fail");
            return resultMap;
        }
    }

    @GetMapping(value = "/document")
    public ResultVo getDocument(@RequestParam("folderId") Integer folderId) {
        if (folderId == null) {
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "文件夹id不能为空");
        }
        return ResultVoUtil.success(iDocumentServer.getDocuments(folderId));
    }

    @GetMapping(value = "/document/page")
    public ResultVo getDocumentPage(@RequestParam("folderId") Integer folderId,
                                    @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam("currentPage") Integer currentPage){
        return  ResultVoUtil.success(iDocumentServer.getDocumentPages(folderId,currentPage,pageSize));
    }

    @DeleteMapping(value = "/document")
    public ResultVo deleteDocument(@RequestParam("id") Integer id, @RequestParam("key") String key, HttpServletRequest request) {
        if (id == null) {
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "id不能为空");
        }
        if (StringUtils.isEmpty(key)) {
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "key不能为空");
        }
        iDocumentServer.deleteDocument(id, getUser(request).getId(), key);
        return ResultVoUtil.success();
    }


    @PutMapping(value = "/document/rename")
    public ResultVo renameDocument(@RequestParam("id") Integer id, @RequestParam("name") String name, HttpServletRequest request) {
        iDocumentServer.renameDocument(id, name, getUser(request).getId());
        return ResultVoUtil.success();
    }

    private User getUser(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return (User) session.getAttribute(SESSION_USER);
    }
}
